include('shared.lua') 

function ENT:Draw()
	self.Entity:DrawModel()
end


function ATMPin()

	local ATMPin = vgui.Create( "DFrame" )
	ATMPin:SetSize( 375, 230 )
	ATMPin:SetTitle( "Automated Teller Machine" )
	ATMPin:SetSizable( false )
	ATMPin:Center()
	ATMPin:MakePopup()
	
	local panel = vgui.Create( "DPanel", ATMPin )
	panel:SetSize( ATMPin:GetWide() - 10, ATMPin:GetTall() - 30 )
	panel:SetPos( 5, 25 )
	function panel.Paint() 
		surface.SetDrawColor( 0, 0, 0, 235 )
		surface.DrawRect( 0, 0, panel:GetWide(), panel:GetTall() )
	end
	 
	local ATMText = vgui.Create( "DLabel", ATMPin )
	ATMText:SetText( "Welcome to the ATM machine." )
	ATMText:SizeToContents()
	ATMText:SetPos( ((ATMPin:GetWide() / 2) - (ATMText:GetWide() / 2 )), 35 )
	ATMText:SetColor( Color( 255, 255, 255, 175 ) )

	local ATMText2 = vgui.Create( "DLabel", ATMPin )
	ATMText2:SetText( "Please register a new account below." )
	ATMText2:SizeToContents()
	ATMText2:SetPos( ((ATMPin:GetWide() / 2) - (ATMText2:GetWide() / 2 )), 50 )
	ATMText2:SetColor( Color( 255, 255, 255, 175 ) )
	
	
	

-- Registration	
	
	local ATMText3 = vgui.Create( "DLabel", ATMPin )
	ATMText3:SetText( "Name:" )
	ATMText3:SizeToContents()
	ATMText3:SetPos( 70, 90 )
	ATMText3:SetColor( Color( 255, 255, 255, 175 ) )

	local ATMText4 = vgui.Create( "DLabel", ATMPin )
	ATMText4:SetText( "Age:" )
	ATMText4:SizeToContents()
	ATMText4:SetPos( 70, 110 )
	ATMText4:SetColor( Color( 255, 255, 255, 175 ) )	
	
	local ATMText4 = vgui.Create( "DLabel", ATMPin )
	ATMText4:SetText( "PIN:" )
	ATMText4:SizeToContents()
	ATMText4:SetPos( 70, 130 )
	ATMText4:SetColor( Color( 255, 255, 255, 175 ) )

	local ATMName = vgui.Create( "DLabel", ATMPin )
	ATMName:SetText( LocalPlayer():Nick() )
	ATMName:SizeToContents()
	ATMName:SetPos( 110, 90 )
	ATMName:SetColor( Color( 255, 255, 255, 175 ) )		
	
	local ATMAge = vgui.Create("DTextEntry")
	ATMAge:SetParent( ATMPin )
	ATMAge:SetSize( 140, 15 )
	ATMAge:SetPos( 110, 110 )
	ATMAge:SetNumeric(true)
	ATMAge.OnTextChanged = function() if string.len(ATMAge:GetValue()) > 2 then ATMAge:SetValue(string.sub(ATMAge:GetValue() , 0 , 2)) end surface.PlaySound("Simplistic/ATM.wav") end
	
	local ATMPinNumber = vgui.Create("DTextEntry")
	ATMPinNumber:SetParent( ATMPin )
	ATMPinNumber:SetSize( 140, 15 )
	ATMPinNumber:SetPos( 110, 130 )
	ATMPinNumber:SetNumeric(true)
	ATMPinNumber.OnTextChanged = function() if string.len(ATMPinNumber:GetValue()) > 4 then ATMPinNumber:SetValue(string.sub(ATMPinNumber:GetValue() , 0 , 4)) end surface.PlaySound("Simplistic/ATM.wav") end	
	
-- End of Registration



	
	local ATMDepositeButton = vgui.Create( "DButton" )
	ATMDepositeButton:SetParent( ATMPin )
	ATMDepositeButton:SetText( "Save details" )
	ATMDepositeButton:SetSize( 150, 20 )
	ATMDepositeButton:SetPos( (ATMPin:GetWide() /2) - (ATMDepositeButton:GetWide() / 2), 185 )
	ATMDepositeButton.DoClick = function()
	
	surface.PlaySound("Simplistic/ATM2.wav")
	
		if (ATMPinNumber:GetValue() == "" or nil) and (ATMAge:GetValue() == "" or nil) then
			GAMEMODE:AddNotify("Error: You need to input your age and PIN!", NOTIFY_ERROR, 5)
			ATMPin:Close()	
			return
		end
	
		if ATMAge:GetValue() == "" or nil then
			GAMEMODE:AddNotify("Error: You need to input your age!", NOTIFY_ERROR, 5)
			ATMPin:Close()
			return
		end
		
		if ATMPinNumber:GetValue() == "" or nil then
			GAMEMODE:AddNotify("Error: You need to input your PIN!", NOTIFY_ERROR, 5)
			ATMPin:Close()
			return			
		end
		
		if string.len(ATMAge:GetValue()) > 2 then
			GAMEMODE:AddNotify("Error: You are not older than 99!", NOTIFY_ERROR, 5)
			ATMPin:Close()
			return			
		end			
		
		if string.len(ATMPinNumber:GetValue()) < 4 then
			GAMEMODE:AddNotify("Error: You need to have 4 digits in your PIN!", NOTIFY_ERROR, 5)
			ATMPin:Close()
			return			
		elseif string.len(ATMPinNumber:GetValue()) > 4 then
			GAMEMODE:AddNotify("Error: You have more than 4 digits in your PIN!", NOTIFY_ERROR, 5)
			ATMPin:Close()
			return				
		end
		
		ATMAreYouSure(ATMPinNumber:GetValue())
		ATMPin:Close()
		
		
	end
end

usermessage.Hook("ATMPin", ATMPin)


function ATMAreYouSure(Possible)
	local AYS = vgui.Create( "DFrame" )
	AYS:SetSize( 375, 120 )
	AYS:SetTitle( "Automated Teller Machine" )
	AYS:SetSizable( false )
	AYS:Center()
	AYS:MakePopup()

	local AYSPanel = vgui.Create( "DPanel", AYS )
	AYSPanel:SetSize( AYS:GetWide() - 10, AYS:GetTall() - 30 )
	AYSPanel:SetPos( 5, 25 )
	function AYSPanel.Paint() 
		surface.SetDrawColor( 0, 0, 0, 235 )
		surface.DrawRect( 0, 0, AYSPanel:GetWide(), AYSPanel:GetTall() )
	end
	
	local AYSText = vgui.Create( "DLabel", AYS )
	AYSText:SetText( "Are you sure you wish to set your PIN to '"..Possible.."'?" )
	AYSText:SizeToContents()
	AYSText:SetPos( ((AYS:GetWide() / 2) - (AYSText:GetWide() / 2 )), 35 )
	AYSText:SetColor( Color( 255, 255, 255, 175 ) )	
	
	local AYSDepositeButton = vgui.Create( "DButton" )
	AYSDepositeButton:SetParent( AYS )
	AYSDepositeButton:SetText( "Yes" )
	AYSDepositeButton:SetPos( 25, 80 )
	AYSDepositeButton:SetSize( 150, 20 )
	AYSDepositeButton.DoClick = function()
		surface.PlaySound("Simplistic/ATM2.wav")
		LocalPlayer():ConCommand("SavePIN "..Possible)
		AYS:Close()
	end
	 
	local AYSWithdrawButton = vgui.Create( "DButton" )
	AYSWithdrawButton:SetParent( AYS )
	AYSWithdrawButton:SetText( "No" )
	AYSWithdrawButton:SetPos( 200, 80 )
	AYSWithdrawButton:SetSize( 150, 20 )
	AYSWithdrawButton.DoClick = function()
		surface.PlaySound("Simplistic/ATM2.wav")
		ATMPin()
		AYS:Close()
	end
end


function ATMDDerma()
	local ATM = vgui.Create( "DFrame" )
	ATM:SetSize( 375, 190 )
	ATM:SetTitle( "Automated Teller Machine" )
	ATM:SetSizable( false )
	ATM:Center()
	ATM:MakePopup()
	
	local panel = vgui.Create( "DPanel", ATM )
	panel:SetSize( ATM:GetWide() - 10, ATM:GetTall() - 30 )
	panel:SetPos( 5, 25 )
	function panel.Paint() 
		surface.SetDrawColor( 0, 0, 0, 235 )
		surface.DrawRect( 0, 0, panel:GetWide(), panel:GetTall() )
	end
	
	local AmountEntry = vgui.Create("DTextEntry")
	AmountEntry:SetParent( ATM )
	AmountEntry:SetPos( 25, 60 )
	AmountEntry:SetSize( 325, 20 )	 
	AmountEntry:SetNumeric(true)
	AmountEntry.OnTextChanged = function() surface.PlaySound("Simplistic/ATM.wav") end
	 
	local Text = vgui.Create( "DLabel", ATM )
	Text:SetText( "Please state the amount of money for this transaction:" )
	Text:SizeToContents()
	Text:SetPos( ((ATM:GetWide() / 2) - (Text:GetWide() / 2 )), 35 )
	Text:SetColor( Color( 255, 255, 255, 175 ) )

	local Text = vgui.Create( "DLabel", ATM )
	Text:SetText( "Your Current Balance: $"..LocalPlayer():GetNWInt("ATMCash") )
	Text:SizeToContents()
	Text:SetPos( ((ATM:GetWide() / 2) - (Text:GetWide() / 2 )), 90 )
	Text:SetColor( Color( 255, 255, 255, 175 ) )		 
	 
	local DepositeButton = vgui.Create( "DButton" )
	DepositeButton:SetParent( ATM )
	DepositeButton:SetText( "Deposit" )
	DepositeButton:SetPos( 25, 140 )
	DepositeButton:SetSize( 150, 20 )
	DepositeButton.DoClick = function()
		surface.PlaySound("Simplistic/ATM2.wav")
		
		if AmountEntry:GetValue() != "" or nil then
			LocalPlayer():ConCommand("_Deposit_ "..AmountEntry:GetValue())
			ATM:Close()
		else
			GAMEMODE:AddNotify("Error: You need to input a value!", NOTIFY_ERROR, 5)
			ATM:Close()
		end
	end
	 
	local WithdrawButton = vgui.Create( "DButton" )
	WithdrawButton:SetParent( ATM )
	WithdrawButton:SetText( "Withdraw" )
	WithdrawButton:SetPos( 200, 140 )
	WithdrawButton:SetSize( 150, 20 )
	WithdrawButton.DoClick = function()
		surface.PlaySound("Simplistic/ATM2.wav")
	
		if AmountEntry:GetValue() != "" or nil then
			LocalPlayer():ConCommand("_Withdraw_ "..AmountEntry:GetValue())
			ATM:Close()
		else
			GAMEMODE:AddNotify("Error: You need to input a value!", NOTIFY_ERROR, 5)
			ATM:Close()
		end
	end
end

usermessage.Hook("ATMMain", ATMDDerma)


function ATMFirstEntrys()

	local ATM = vgui.Create( "DFrame" )
	ATM:SetSize( 375, 190 )
	ATM:SetTitle( "Automated Teller Machine" )
	ATM:SetSizable( false )
	ATM:Center()
	ATM:MakePopup()
	
	local panel = vgui.Create( "DPanel", ATM )
	panel:SetSize( ATM:GetWide() - 10, ATM:GetTall() - 30 )
	panel:SetPos( 5, 25 )
	function panel.Paint() 
		surface.SetDrawColor( 0, 0, 0, 235 )
		surface.DrawRect( 0, 0, panel:GetWide(), panel:GetTall() )
	end
	 
	local ATMText = vgui.Create( "DLabel", ATM )
	ATMText:SetText( "Welcome to the ATM machine." )
	ATMText:SizeToContents()
	ATMText:SetPos( ((ATM:GetWide() / 2) - (ATMText:GetWide() / 2 )), 35 )
	ATMText:SetColor( Color( 255, 255, 255, 175 ) )

	local ATMText2 = vgui.Create( "DLabel", ATM )
	ATMText2:SetText( "Please enter your PIN below:" )
	ATMText2:SizeToContents()
	ATMText2:SetPos( ((ATM:GetWide() / 2) - (ATMText2:GetWide() / 2 )), 50 )
	ATMText2:SetColor( Color( 255, 255, 255, 175 ) )

	local AmountEntry = vgui.Create("DTextEntry")
	AmountEntry:SetParent( ATM )
	AmountEntry:SetSize( 325, 20 )
	AmountEntry:SetPos( ((ATM:GetWide() / 2) - (AmountEntry:GetWide() / 2 )), 85 ) 	
	AmountEntry:SetNumeric(true)
	AmountEntry.OnTextChanged = function() surface.PlaySound("Simplistic/ATM.wav") end
	
	local ATMDepositeButton = vgui.Create("DButton")
	ATMDepositeButton:SetParent( ATM )
	ATMDepositeButton:SetText( "Submit" )
	ATMDepositeButton:SetSize( 150, 20 )
	ATMDepositeButton:SetPos( (ATM:GetWide() /2) - (ATMDepositeButton:GetWide() / 2), 120 )
	ATMDepositeButton.DoClick = function()	
		surface.PlaySound("Simplistic/ATM2.wav")
		LocalPlayer():ConCommand("VerifyPIN "..AmountEntry:GetValue())
		ATM:Close()
	end
end

usermessage.Hook("ATMFirstEntry", ATMFirstEntrys)